{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:18:36.665631Z",
     "start_time": "2025-01-08T07:18:36.129279Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "id": "f434af668bb57514",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 3 常见的Index种类",
   "id": "d97d4124f822684a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:18:36.673383Z",
     "start_time": "2025-01-08T07:18:36.666648Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# •Index，索引  可以是各种类型\n",
    "# •Int64Index，整数索引\n",
    "# •MultiIndex，层级索引，难度较大\n",
    "# •DatetimeIndex，时间戳类型\n",
    "\n",
    "ser_obj = pd.Series(range(5), index=['a', 'b', 'c', 'd', 'e'])\n",
    "print(ser_obj)\n",
    "print(ser_obj.index)"
   ],
   "id": "c196b2746cb460bd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:18:36.678422Z",
     "start_time": "2025-01-08T07:18:36.674392Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行索引，不仅可以用索引名，可以用索引位置或来取\n",
    "print(ser_obj[0])  # 位置索引\n",
    "print(ser_obj['a'])  # 索引名"
   ],
   "id": "ee5df5db9de37bf9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\32952\\AppData\\Local\\Temp\\ipykernel_19820\\1525916861.py:2: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  print(ser_obj[0]) # 位置索引\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:25:52.530697Z",
     "start_time": "2025-01-08T07:25:52.524882Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 推荐使用iloc和loc，iloc用位置索引，loc用索引名\n",
    "print(ser_obj.iloc[0])  # 位置索引\n",
    "print(ser_obj.loc['a'])  # 索引名\n",
    "print(ser_obj.iloc[1])\n",
    "print(ser_obj.loc['b'])  # 索引名"
   ],
   "id": "6f2320e4e7b92e71",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:20:16.428705Z",
     "start_time": "2025-01-08T07:20:16.422321Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 切片索引\n",
    "print(ser_obj.iloc[1:3])  #索引位置取数据，左闭右开\n",
    "print(ser_obj.loc['b':'d'])  #索引名取数据，左闭右闭"
   ],
   "id": "ffc19bea5e7060c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:21:24.055829Z",
     "start_time": "2025-01-08T07:21:24.050180Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不连续索引\n",
    "print(ser_obj.iloc[[1, 3, 4]])  # 位置索引\n",
    "print(ser_obj.loc[['b', 'd', 'e']])  # 索引名索引"
   ],
   "id": "326558787c77e6fe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "b    1\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:23:22.569694Z",
     "start_time": "2025-01-08T07:23:22.563743Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "ser_bool = ser_obj > 2\n",
    "print(ser_obj)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(ser_bool)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(ser_obj[ser_bool])  # 取出ser_obj中大于2的元素\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(ser_obj[ser_obj > 2])  # 等价于上一行"
   ],
   "id": "e73f2818a709d055",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n",
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4 Pandas的索引操作",
   "id": "f18cf6469141a83b"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.4 DataFrame索引",
   "id": "61c80a50b50182a0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:35:19.351372Z",
     "start_time": "2025-01-08T07:35:19.342738Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df_obj = pd.DataFrame(np.random.randn(5, 4), columns=['a', 'b', 'c', 'd'])\n",
    "print(df_obj)"
   ],
   "id": "ff24c69de1b4eebf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0  1.052082 -0.199223  0.027826 -1.465371\n",
      "1 -0.070903 -1.266409 -0.486899 -0.570697\n",
      "2  0.798083  1.704816  0.613932 -1.823053\n",
      "3 -0.246825  0.471097 -0.422954  1.192610\n",
      "4 -0.091836 -0.332805 -1.598216  0.573923\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:39:35.952506Z",
     "start_time": "2025-01-08T07:39:35.943990Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列索引\n",
    "print(df_obj['a'])  # 取出列a的数据\n",
    "print(type(df_obj['a']))  # 输出类型为Series\n",
    "print(\"-\" * 50)\n",
    "print(df_obj[['a']])  # 取出列a的数据\n",
    "print(type(df_obj[['a']]))  # 输出类型为DataFrame\n",
    "print(\"-\" * 50)\n",
    "print(df_obj[['a', 'b']])  # 取出列a和b的数据\n",
    "print(type(df_obj[['a', 'b']]))  # 输出类型为DataFrame"
   ],
   "id": "6dbb20f618f8549",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1.052082\n",
      "1   -0.070903\n",
      "2    0.798083\n",
      "3   -0.246825\n",
      "4   -0.091836\n",
      "Name: a, dtype: float64\n",
      "<class 'pandas.core.series.Series'>\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0  1.052082\n",
      "1 -0.070903\n",
      "2  0.798083\n",
      "3 -0.246825\n",
      "4 -0.091836\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "          a         b\n",
      "0  1.052082 -0.199223\n",
      "1 -0.070903 -1.266409\n",
      "2  0.798083  1.704816\n",
      "3 -0.246825  0.471097\n",
      "4 -0.091836 -0.332805\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "1.loc 标签索引(通过索引标签值获取数据)   左闭右闭[]",
   "id": "199ea876cefa7f80"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "标签索引 loc，建议使用loc，效率更高",
   "id": "3edef03064c0970"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:43:55.489945Z",
     "start_time": "2025-01-08T07:43:55.484317Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series类型\n",
    "ser_obj = pd.Series(range(5), index=['a', 'b', 'c', 'd', 'e'])\n",
    "print(ser_obj)\n",
    "print(\"-\" * 50)\n",
    "print(ser_obj.loc['a'])  # 索引名索引\n",
    "print(\"-\" * 50)\n",
    "print(ser_obj.loc['a':'d'])  # 索引名索引 前闭后闭"
   ],
   "id": "5b00e6919936ed2e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "0\n",
      "--------------------------------------------------\n",
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:50:08.488434Z",
     "start_time": "2025-01-08T07:50:08.479842Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame类型\n",
    "df_obj = pd.DataFrame(np.random.randn(5, 4), columns=list(\"abcd\"), index=list('abcde'))\n",
    "print(df_obj)\n",
    "#           a         b         c         d\n",
    "# a -0.172216 -0.822122  0.025654 -1.857290\n",
    "# b  0.677460 -1.288862  0.304398  1.143204\n",
    "# c  0.312086 -1.030570  0.580459 -2.166289\n",
    "# d  1.013193 -0.009885 -0.175467 -0.016742\n",
    "# e -1.577072  1.442066 -0.939780 -1.907015\n",
    "print(\"-\" * 50)\n",
    "print(df_obj.loc['a'])  # 索引名索引 行索引，取得是行数据\n",
    "# a   -0.172216\n",
    "# b   -0.822122\n",
    "# c    0.025654\n",
    "# d   -1.857290\n",
    "# Name: a, dtype: float64\n",
    "print(\"-\" * 50)\n",
    "print(df_obj['a'])  # 列索引，取得是列数据\n",
    "# a   -0.172216\n",
    "# b    0.677460\n",
    "# c    0.312086\n",
    "# d    1.013193\n",
    "# e   -1.577072\n",
    "# Name: a, dtype: float64"
   ],
   "id": "1f07fda6974b5c2f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -0.172216 -0.822122  0.025654 -1.857290\n",
      "b  0.677460 -1.288862  0.304398  1.143204\n",
      "c  0.312086 -1.030570  0.580459 -2.166289\n",
      "d  1.013193 -0.009885 -0.175467 -0.016742\n",
      "e -1.577072  1.442066 -0.939780 -1.907015\n",
      "--------------------------------------------------\n",
      "a   -0.172216\n",
      "b   -0.822122\n",
      "c    0.025654\n",
      "d   -1.857290\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a   -0.172216\n",
      "b    0.677460\n",
      "c    0.312086\n",
      "d    1.013193\n",
      "e   -1.577072\n",
      "Name: a, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T07:59:56.307215Z",
     "start_time": "2025-01-08T07:59:56.295036Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一个参数索引行，第二个参数是列,loc或者iloc效率高于直接用取下标的方式，前闭后闭\n",
    "print(df_obj.loc[\"a\":\"c\", \"b\":\"d\"])  # 连续索引\n",
    "#           b         c         d\n",
    "# a -0.822122  0.025654 -1.857290\n",
    "# b -1.288862  0.304398  1.143204\n",
    "# c -1.030570  0.580459 -2.166289\n",
    "print(type(df_obj.loc[\"a\":\"c\", \"b\":\"d\"]))  # 输出类型为DataFrame\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_obj.loc[[\"a\", \"c\"], [\"b\", \"d\"]])  # 非连续索引\n",
    "#           b         d\n",
    "# a -0.822122 -1.857290\n",
    "# c -1.030570 -2.166289\n",
    "print(type(df_obj.loc[[\"a\", \"c\"], [\"b\", \"d\"]]))  # 输出类型为DataFrame\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_obj.loc[[\"c\"], [\"d\"]])  #取一个值,返回的是DataFrame类型\n",
    "#           d\n",
    "# c -2.166289\n",
    "print(type(df_obj.loc[[\"c\"], [\"d\"]]))  # 输出类型为DataFrame\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_obj.loc[\"c\", \"d\"])  # 位置索引\n",
    "# -2.166289\n",
    "print(type(df_obj.loc[\"c\", \"d\"]))  # 输出类型为<class 'numpy.float64'>"
   ],
   "id": "882e7747e6abbef8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a -0.822122  0.025654 -1.857290\n",
      "b -1.288862  0.304398  1.143204\n",
      "c -1.030570  0.580459 -2.166289\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "          b         d\n",
      "a -0.822122 -1.857290\n",
      "c -1.030570 -2.166289\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "          d\n",
      "c -2.166289\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "-2.1662891517530913\n",
      "<class 'numpy.float64'>\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "iloc 位置索引(通过位置索引获取数据) 左闭右开[)",
   "id": "e94c4952afacb435"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:07:28.189868Z",
     "start_time": "2025-01-08T08:07:28.183968Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series类型\n",
    "ser_obj = pd.Series(range(5), index=['a', 'b', 'c', 'd', 'e'])\n",
    "print(ser_obj)\n",
    "print(\"-\" * 50)\n",
    "print(ser_obj.iloc[1])  # 索引名索引\n",
    "print(\"-\" * 50)\n",
    "print(ser_obj.iloc[1:3])  # 索引名索引 前闭后开[)"
   ],
   "id": "6ffbffa6f2ec6fb5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "1\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:17:21.329946Z",
     "start_time": "2025-01-08T08:17:21.316320Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame类型\n",
    "df_obj = pd.DataFrame(np.random.randn(5, 4), columns=list(\"abcd\"), index=list('abcde'))\n",
    "print(df_obj)\n",
    "print(\"-\" * 50)\n",
    "print(df_obj.iloc[0:3, 1:4])  # 连续索引\n",
    "#           b         c         d\n",
    "# a -0.822122  0.025654 -1.857290\n",
    "# b -1.288862  0.304398  1.143204\n",
    "# c -1.030570  0.580459 -2.166289\n",
    "print(type(df_obj.iloc[0:3, 1:4]))  # 输出类型为DataFrame\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_obj.iloc[[0, 2], [1, 3]])  # 非连续索引\n",
    "#           b         d\n",
    "# a -0.822122 -1.857290\n",
    "# c -1.030570 -2.166289\n",
    "print(type(df_obj.iloc[[0, 2], [1, 3]]))  # 输出类型为DataFrame\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_obj.iloc[[2], [3]])  #取一个值,返回的是DataFrame类型\n",
    "#           d\n",
    "# c -2.166289\n",
    "print(type(df_obj.iloc[[2], [3]]))  # 输出类型为DataFrame\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_obj.iloc[2, 3])  # 位置索引\n",
    "# -2.166289\n",
    "print(type(df_obj.iloc[2, 3]))  # 输出类型为<class 'numpy.float64'>\n"
   ],
   "id": "4d191d698c333d4f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -0.146188 -1.564669 -0.649166  0.906560\n",
      "b -0.434921  0.288020  0.926147  0.325602\n",
      "c -1.188064  1.445891 -0.236027  0.750840\n",
      "d -1.698187  0.829832  1.888023  1.530834\n",
      "e  1.265608  1.804327 -0.516611  0.517831\n",
      "--------------------------------------------------\n",
      "          b         c         d\n",
      "a -1.564669 -0.649166  0.906560\n",
      "b  0.288020  0.926147  0.325602\n",
      "c  1.445891 -0.236027  0.750840\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "          b        d\n",
      "a -1.564669  0.90656\n",
      "c  1.445891  0.75084\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "         d\n",
      "c  0.75084\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n",
      "0.750839663368437\n",
      "<class 'numpy.float64'>\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:22:02.801949Z",
     "start_time": "2025-01-08T08:22:02.792684Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#没有设置行和列索引的DataFrame，iloc和loc的区别\n",
    "df_obj = pd.DataFrame(np.random.randn(5, 4))\n",
    "print(df_obj)\n",
    "print(\"-\" * 50)\n",
    "print(df_obj.iloc[0:2])  # 左闭右开 [) 打印了2行\n",
    "print(\"-\"*50)\n",
    "print(df_obj.loc[0:2])  # 左闭右闭 [] 打印了3行\n"
   ],
   "id": "f4a6c0f0aab2103c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.616861  0.628215 -0.577016  1.107643\n",
      "1 -0.312304  0.926840  0.563429  0.237946\n",
      "2  0.051344 -0.152888  0.134774  0.386753\n",
      "3 -0.494500 -2.633042  0.205873 -0.094401\n",
      "4  0.200614  0.512322  1.112411  0.790520\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.616861  0.628215 -0.577016  1.107643\n",
      "1 -0.312304  0.926840  0.563429  0.237946\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.616861  0.628215 -0.577016  1.107643\n",
      "1 -0.312304  0.926840  0.563429  0.237946\n",
      "2  0.051344 -0.152888  0.134774  0.386753\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5 对齐运算",
   "id": "164b06b5998c3ef3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:32:07.107070Z",
     "start_time": "2025-01-08T08:32:07.101251Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 两个长度不同的一维ndarray相加 \n",
    "a1=np.array([1,2,3,4,5])\n",
    "a2=np.array([1])\n",
    "print(a1)\n",
    "# [1 2 3 4 5]\n",
    "print(a2)\n",
    "# [1]\n",
    "a3=a1+a2 \n",
    "print(a3)\n",
    "# [2 3 4 5 6]\n",
    "\n",
    "# a4=np.array([1,2])\n",
    "# a5=a1+a4 # 报错\n",
    "# print(a5) \n",
    "# [ 2  4  6  8 10]"
   ],
   "id": "fc354d1c9f9e1ee2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "[1]\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:28:53.547854Z",
     "start_time": "2025-01-08T08:28:53.540318Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series的对齐运算\n",
    "s1=pd.Series(range(10,20),index=range(10))\n",
    "s2=pd.Series(range(5),index=range(5))\n",
    "\n",
    "print(s1)\n",
    "print(\"-\"*50)\n",
    "print(s2)\n",
    "print(\"-\"*50)\n",
    "\n",
    "s3=s1+s2\n",
    "print(s3)   # 缺失数据默认是NaN  np.nan\n",
    "print(\"-\"*50) "
   ],
   "id": "b731291ae1dec0f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "0    0\n",
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "4    4\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "0    10.0\n",
      "1    12.0\n",
      "2    14.0\n",
      "3    16.0\n",
      "4    18.0\n",
      "5     NaN\n",
      "6     NaN\n",
      "7     NaN\n",
      "8     NaN\n",
      "9     NaN\n",
      "dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:42:44.861266Z",
     "start_time": "2025-01-08T08:42:44.854724Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 说明NaN 就是 np.nan\n",
    "print(np.isnan(s3[5])) # True\n",
    "\n",
    "# fill_value=0 填充缺失值\n",
    "print(s2.add(s1,fill_value=0))\n",
    "print(\"-\"*50)\n",
    "print(s2.sub(s1,fill_value=0))"
   ],
   "id": "3ffc071e2d8b535e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "0    10.0\n",
      "1    12.0\n",
      "2    14.0\n",
      "3    16.0\n",
      "4    18.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "0   -10.0\n",
      "1   -10.0\n",
      "2   -10.0\n",
      "3   -10.0\n",
      "4   -10.0\n",
      "5   -15.0\n",
      "6   -16.0\n",
      "7   -17.0\n",
      "8   -18.0\n",
      "9   -19.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:48:25.949121Z",
     "start_time": "2025-01-08T08:48:25.937466Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame的对齐运算\n",
    "df1=pd.DataFrame(np.ones((2,2)),columns=['a','b'])\n",
    "df2=pd.DataFrame(np.ones((3,3)),columns=['a','b','c'])\n",
    "\n",
    "print(df1)\n",
    "print(\"-\"*50)\n",
    "print(df2)\n",
    "print(\"-\"*50)\n",
    "\n",
    "df3=df1+df2 # 缺失数据默认是NaN  np.nan\n",
    "print(df3) \n",
    "print(\"-\"*50)\n",
    "\n",
    "print(df1.add(df2,fill_value=0)) # 未对齐的数据将和填充值做运算\n"
   ],
   "id": "46cb7ea76328ce35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "--------------------------------------------------\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "--------------------------------------------------\n",
      "     a    b   c\n",
      "0  2.0  2.0 NaN\n",
      "1  2.0  2.0 NaN\n",
      "2  NaN  NaN NaN\n",
      "--------------------------------------------------\n",
      "     a    b    c\n",
      "0  2.0  2.0  1.0\n",
      "1  2.0  2.0  1.0\n",
      "2  1.0  1.0  1.0\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总结：没对齐的元素，默认填充NaN，对齐运算时，fill_value参数可以指定填充值。",
   "id": "5a23bb3c0ec730d5"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
